package com.satchi.dao.imp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.satchi.commons.BaseDao;
import com.satchi.dao.ReceiptDao;
import com.satchi.hib.pojo.FinCustomer;
import com.satchi.hib.pojo.FinReceipt;
import com.satchi.hib.pojo.FinUser;
import com.satchi.struts.ActionHelper;
import com.satchi.struts.form.FinReceiptExcelForm;

@SuppressWarnings("serial")
public class GetReceiptDaoImp extends BaseDao implements ReceiptDao {

	public int getAllCount() {
		String sql=" select * from FinReceipt";
		int rows = getCount(sql);
		return rows;
	}
	
	public int getSearchCount(String key) {
		String sql = "select count(1) from FinReceipt a " +
				"join finCustomer b on a.customerId = b.customerId " +
				"where b.customer like '%" + key + "%'";
		return getSqlCount(sql);
	}
	
	@SuppressWarnings("unchecked")
	public List getPageListNullpar(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		String hql = "select re.receiptId," +
		"re.receiptDate," +
		"re.finCustomer," +
		"re.writeDate," +
		"re.gatherWay," +
		"re.receiptPrince," +
		"re.receiptRemark," +
		"re.finUser from FinReceipt as re order by receiptId";

		return getPageList(hql, pageNo, pageSize);
	}
	
	public List<FinReceipt> getPageList(int pageNo, int pageSize) {
		String hql = "from FinReceipt as re order by receiptId";
		return getPageList(hql, pageNo, pageSize);
	}

	public List<FinReceipt> getPage(Object[] params, int pageNo, int pageSize) {		
		String hql = "from FinReceipt as re where " +
				" and re.finCustomer.customer like ?"+
				" and gatherWay = ?" +
				" and receiptDate >= ? and receiptDate < ?"+
				" and writeDate between ? and ? order by re.receiptId";
		return getPageList(hql, params, pageNo, pageSize);
	}
	
	public List<FinReceipt> getExcel(FinReceiptExcelForm form) throws ParseException {	
		StringBuffer buffer = new StringBuffer("from FinReceipt as re");
		String customer = form.getCustomer();
		List<Object> params = new ArrayList<Object>();
		if (!ActionHelper.IsEmpty(customer)) {
			ActionHelper.SafeAppend(buffer, " re.finCustomer.customer like ? ");
			params.add('%' + customer + '%');
		}
		String[] gatherWay = form.getGatherWay();
		if (gatherWay != null && gatherWay.length == 1) {
			ActionHelper.SafeAppend(buffer, " gatherWay = ? ");
			params.add(gatherWay[0]);
		}
		int[] isClosed = form.getIsClosed();
		if (isClosed != null && isClosed.length == 1) {
			ActionHelper.SafeAppend(buffer, " isClosed = ? ");
			params.add(isClosed[0] == 1);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		String receiptDate_Begin = form.getReceiptDate_Begin();
		if (!ActionHelper.IsEmpty(receiptDate_Begin)) {
			ActionHelper.SafeAppend(buffer, " receiptDate >= ? ");
			params.add(sdf.parse(receiptDate_Begin));
		}
		String receiptDate_End = form.getReceiptDate_End();
		if (!ActionHelper.IsEmpty(receiptDate_End)) {
			ActionHelper.SafeAppend(buffer, " receiptDate <= ? ");
			params.add(sdf.parse(receiptDate_End));
		}
		String writeDate_Begin = form.getWriteDate_Begin();
		if (!ActionHelper.IsEmpty(writeDate_Begin)) {
			ActionHelper.SafeAppend(buffer, " writeDate >= ? ");
			params.add(sdf.parse(writeDate_Begin));
		}
		String writeDate_End = form.getWriteDate_End();
		if (!ActionHelper.IsEmpty(writeDate_End)) {
			ActionHelper.SafeAppend(buffer, " writeDate <= ? ");
			params.add(sdf.parse(writeDate_End));
		}
		
		return runHql(buffer.toString(), params.toArray());
	}

	public boolean save(FinReceipt fr) {
		return saveOrUpdate(fr);
	}

	public List<FinUser> runResulthql(Object[] params) {
		// TODO Auto-generated method stub
		String hql = "from FinUser where uname=?";
		return runResultHql(hql,params);
	}

	public List<FinCustomer> runResulthqlCus(Object[] params) {
		// TODO Auto-generated method stub
		String hql = "from FinCustomer where customer=?";
		return runResultHql(hql,params);
	}
}
